#include "Alerts.h";

#include "Auto_Top.h";

extern LiquidCrystal     lcd;
extern PROGMEM const char *dosing_pumps_name_table[];
extern Auto_Top auto_top;

Alerts::Alerts(){
  pinMode(Alerts_Horn_Pin, OUTPUT);
}

bool Alerts::Is_Alert_On(){
  return  (Number_OF_Alerts + auto_top.Get_Number_Of_Print_Errors() ) != 0;
}
byte Alerts::Get_Number_OF_Alerts(){
  return (Number_OF_Alerts + auto_top.Get_Number_Of_Print_Errors() ) ;
}

byte Alerts::Get_Number_Of_Print_Line(){
  byte Alerts = Number_OF_Alerts + auto_top.Get_Number_Of_Print_Errors();
  if ( Alerts == 0 ) {
    return 1;
  } 
  else {
    return Alerts;
  }
}

byte Alerts::Clear_Alert(byte Line_Num){
  Print_Clear(false,Line_Num);
  return Get_Number_Of_Print_Line();
}

void Alerts::Print(byte Line_Num){
  Print_Clear(true,Line_Num);
}

void Alerts::Print_Clear(bool Print , byte Line_Num){
  if ( Number_OF_Alerts == 0  && (auto_top.Get_Number_Of_Print_Errors() == 0) ) {
    strcpy_P(buffer, (char*)pgm_read_word(&(  gen_string_table[  NO_ALERTS_STRING ]     )));
    lcd.print(buffer );
    //lcd.print("No Alerts");
  } 
  else {
    byte alert_num = 0;
    byte Auto_Top_Line = 0;

    for ( byte i= 0 ; i < NUMBER_OF_DOSING_PUMPS ; ++i ){
      if ( Dosing_Pump_Alert[i] ){
        if ( alert_num == Line_Num){
          if ( Print ){
            strcpy_P(buffer, (char*)pgm_read_word(&(  dosing_pumps_name_table[  i ]     )));
            lcd.print(buffer );
          } 
          else {
            Dosing_Pump_Alert[i] = false;
            --Number_OF_Alerts;
          }
          return;
        }
        ++alert_num;
      }
    }


    for ( byte i= 0 ; i < auto_top.Get_Number_Of_Print_Errors()  ; ++i ){
      if ( alert_num == Line_Num){
        auto_top.Print_Error(Auto_Top_Line);
      }
      ++Auto_Top_Line;
      ++alert_num;
    }
  }
}

void Alerts::Turn_Horn_on(){
  digitalWrite(Alerts_Horn_Pin, HIGH);    
}

void Alerts::Turn_Horn_off(){
  digitalWrite(Alerts_Horn_Pin, LOW);    
}










